package com.smart.web.controller.system;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.smart.common.constant.GlobalConstants;
import com.smart.common.core.domain.R;
import com.smart.common.core.page.PageQuery;
import com.smart.common.utils.DateUtils;
import com.smart.system.domain.Alarm;
import com.smart.system.service.IAlarmService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/system/alarm")
@Api(tags = "告警")
public class AlarmController {
    @Autowired
    private IAlarmService alarmService;

    /**
     * 获取告警列表
     */
    @GetMapping("list")
    public R<List<Alarm>> list() {
        List<Alarm> list = alarmService.list(Wrappers.lambdaQuery(Alarm.class).eq(Alarm::getStatus, GlobalConstants.YES_CHECKED));
        return R.ok(Alarm.distinct(list));
    }

    /**
     * 消息已读
     */
    @PutMapping("read")
    public R<Boolean> read(Integer type,Integer toId,Integer message) {
        boolean alarm = alarmService.update(Wrappers.lambdaUpdate(Alarm.class)
                .eq(Alarm::getType, type)
                .eq(Alarm::getToId, toId)
                .eq(Alarm::getMessage, message)
                .set(Alarm::getStatus, GlobalConstants.NO_CHECKED));
        return R.ok(alarm);
    }
}
